<template>
  <div class="login-page">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>登录</span>
        </div>
      </template>
      <el-form :model="form" label-width="100px" @submit.prevent="handleLogin">
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <!-- 添加native-type="submit"用于触发handleLogin-->
          <el-button type="primary" native-type="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.login-page{
  display: flex;
  justify-content: center;
  align-items: center;
  height: 33vh;
  background: #f0f2f5;
}

.box-card{
  width: 480px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header{
  text-align: center;
}

</style>

<script>
import {ref} from "vue";
import {ElNotification} from "element-plus";
//import {useRoute} from "vue-router";
import router  from "../router/index.js";
export default {
  setup() {
    const form = ref({username: "", password: ""});
    //获取路由实例
    //const router = useRoute();
    const handleLogin = () => {
      //固定验证用户名和密码（真实环境需连接数据库并签权）
      if (form.value.username === "admin"&&form.value.password === "admin") {
        //验证成功网存储认证状态localStorage
        localStorage.setItem("isAuthenticated",'true');
        ElNotification.success({
          title:'成功',
          message:'登陆成功',
          type: 'success'
        })
        //验证成功就进行跳转
        router.push('/Demo2')
      }else {
        ElNotification({
          title:'失败',
          message:'用户名或密码错误，请重试',
          type:'error'
        })
      }
    }
    return {form,handleLogin}
  }
}
</script>